
%include "ASMimplibs.sas";


*** Modified 2014 to use CART data;
*** Modified 04/14/2015 to use CART data with ALL imputed variables (except TE, SW);

%macro estimate_demand(prod,dataset);

/* Need to get log income variable from final FHS datasets and merge with the CART-completed data
   (or the CART-predicted dataset). : */

  data all&prod._post&dataset; set fhs7797.all&prod._post&dataset;   
   prod = "&prod"; 
  run;

  data &prod._inc (keep = ppn year lincome); set fhspen50.all&prod.f; run;
  proc sort data=&prod._inc; by ppn year; run;
  proc sort data=all&prod._post&dataset; by ppn year; run;
  data all&prod._post&dataset; merge all&prod._post&dataset &prod._inc; by ppn year; run;

  /* Create year dummies. */
  data &prod; set all&prod._post&dataset;
    if year=1977 then yrdum77=1; else yrdum77=0;
    if year=1982 then yrdum82=1; else yrdum82=0;
    if year=1987 then yrdum87=1; else yrdum87=0;
    if year=1992 then yrdum92=1; else yrdum92=0;
    if year=1997 then yrdum97=1; else yrdum97=0;
   IF _IMPUTE_=. then delete;   * Added 04/15/2015 to avoid problem with flooring obs in 1997;
  run;

  proc sort data=&prod; by _impute_ ; run;

   proc model data=&prod ; 
    by _impute_;
    endogenous lprice2 lqphy;
    parameters  alpha0 alpha1 alpha82 alpha87 alpha92 alpha97 alpha2;
    lqphy = alpha0 + alpha1*lprice2 + alpha82*yrdum82 +alpha87*yrdum87 +alpha92*yrdum92 +alpha97*yrdum97 
	     +alpha2*lincome;
    id ppn year;
    instruments yrdum82 yrdum87 yrdum92 yrdum97 lincome ltfpphy; 
    fit lqphy / 2sls out=resids_&prod outresid outest=parms_&prod;
    title "Demand equation estimates for &prod";run;
   run;

  /* Note: the residuals dataset includes the lincome variable,
     so we can use that to construct the demand shocks. */

   /* Merge the parameter estimates with the residuals so that we can
     "add back in" the local income effect (see FHS (2008), p. 408) */
     proc sort data=parms_&prod; by _impute_; run;
     proc sort data=resids_&prod; by _impute_; run;

   /* lqphy is the residual from the demand system estimation */

     data dshk99_&prod (keep = _impute_ ppn year dshk99);
       merge parms_&prod resids_&prod;
       by _impute_ ; 
	dshk99=lqphy+alpha2*lincome;
      run;
  
      proc sort data=dshk99_&prod; by _impute_ ppn year;run;

      /*data fhs7797.dshk99_&prod;*/
      data fhs7797.dshk99_&prod.&dataset;
       set dshk99_&prod;
      run;

  %mend;


/* For sugar, we don't impute, so just use FHS's demand shock. */
/**data allsug_postCART; 
 set fhspen50.allsugf;
  _impute_ = 1; 
run;
**/


**%estimate_demand(sug);  

%estimate_demand(bred,CART);
%estimate_demand(carbon,CART);
%estimate_demand(coff,CART);
%estimate_demand(conc,CART);
%estimate_demand(flr,CART);
%estimate_demand(gas,CART);
%estimate_demand(iceb,CART);
%estimate_demand(icep,CART);
%estimate_demand(ply,CART);
%estimate_demand(box,CART);

/* For sugar, we don't impute, so just use FHS's demand shock. */


/***************
data allsug_postpred; 
 set fhspen50.allsugf;
  _impute_ = 1; 
run;

%estimate_demand(bred,pred);
%estimate_demand(carbon,pred);
%estimate_demand(coff,pred);
%estimate_demand(conc,pred);
%estimate_demand(flr,pred);
%estimate_demand(gas,pred);
%estimate_demand(iceb,pred);
%estimate_demand(icep,pred);
%estimate_demand(ply,pred);
%estimate_demand(box,pred);

**************/

/* data dshk_sug (keep = ppn year dshk99);
 set dshk_sug;
run;
*/
/* data test.demandshk (keep=_impute_ ppn year dshk99); */ 
data demandshk_pool (keep=_impute_ ppn year dshk99); 
 merge dshk99_box dshk99_bred dshk99_carbon dshk99_coff dshk99_conc dshk99_flr 
       dshk99_gas dshk99_iceb dshk99_icep dshk99_ply /* dshk_sug */;
  by _impute_ ppn year;
run;

/*data demandshk_pool;
 set test.demandshk;
run;
*/

proc sort data= demandshk_pool; by year; run;

proc means data=demandshk_pool;
  var dshk99;
by year;
title "dshk99 by year";
run;


proc sort data= demandshk_pool; by _impute_; run;

proc means data=demandshk_pool;
  var dshk99;
by _impute_;
title "dshk99 by implicate";
run;
